﻿using System;
using System.Collections;
using System.Web;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine;

namespace viForum.HttpModules
{
    /// <summary>
    /// The forum url rewriter
    /// </summary>
    public class UrlRewriter : BaseUrlRewriter
    {

        public override string RewriteUrl(HttpApplication httpApp, IEnumerator urlEnum)
        {
            HttpContext context = ((HttpApplication)httpApp).Context;
            return HandleURL(context, urlEnum);
        }

        /*****************************************************************************************/
        // Forum URL = Forum.aspx
        /*****************************************************************************************/
        /*****************************************************************************************/
        // forum URL = Forum/ForumId/ForumTitle .aspx
        /*****************************************************************************************/
        /*****************************************************************************************/
        // thread URL = Forum/Thread/ThreadId/ThreadTitle .aspx
        /*****************************************************************************************/

        private string HandleURL(HttpContext context, IEnumerator enu)        
        {
            string root = WebRoots.GetRelativeWebRoot;
            if (!enu.MoveNext())
            { // Forum URL = Forum.aspx
                return root + "Default.aspx?PageId=" + BaseController.ForumPageId;
            }
            else
            {
                int forumid = 0;
                if (Int32.TryParse(TrimExtension((string)enu.Current), out forumid))
                {
                    Forum forum = ForumController.GetForum(forumid);
                    if (forum != null)
                    {
                        return root + "Default.aspx?PageId=" + BaseController.ForumThreadListPageId + "&ForumId=" + forumid;
                    }
                }
                else
                {
                    // display thread
                    if (TrimExtension((string)enu.Current).ToLower() == "thread")
                    {
                        if (enu.MoveNext())
                        {
                            int threadid = 0;
                            if (Int32.TryParse(TrimExtension((string)enu.Current), out threadid))
                            {
                                Thread thread = ThreadController.GetThread(threadid);
                                if (thread != null)
                                {
                                    return root + "Default.aspx?PageId=" + BaseController.ForumThreadPageId + "&ThreadId=" + threadid + "&ForumId=" + thread.ForumId;
                                }
                            }
                        }
                    }
                    // display thread
                    if (TrimExtension((string)enu.Current).ToLower() == "editor")
                    {
                        if (enu.MoveNext())
                        {
                            switch (((string)enu.Current).ToLower())
                            {
                                case "addthread":
                                    if (enu.MoveNext())
                                        return root + "Default.aspx?PageId=" + BaseController.ForumEditorPageId + "&AddThread=True" + "&ForumId=" + TrimExtension((string)enu.Current);
                                    break;
                                case "addreply":
                                    string strforumid = "";
                                    if (enu.MoveNext())
                                        strforumid = (string)enu.Current;
                                    {
                                        if (enu.MoveNext())
                                            return root + "Default.aspx?PageId=" + BaseController.ForumEditorPageId + "&AddReply=True" + "&ForumId=" + strforumid + "&ThreadId=" + TrimExtension((string)enu.Current);
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            return null;
        }

        private string TrimExtension(string url)
        {
            if (url.EndsWith(".aspx"))
                return url.Remove(url.LastIndexOf('.'));
            else
                return url;
        }
    }
}

